package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.CareerDev;
import model.Employee;
import model.JobRes;

import dao.ProfileDAO;

/**
 * Servlet implementation class manipulateCareerDev
 */
public class manipulateCareerDev extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProfileDAO pDao;  
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public manipulateCareerDev() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			pDao = new ProfileDAO();
			String id = request.getParameter("id");
			System.out.println(id);
			int caDevId = Integer.parseInt(id);
			System.out.println(pDao.removeCareerDev(caDevId));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			pDao = new ProfileDAO();  
			HttpSession session = request.getSession();
			Employee em = (Employee) session.getAttribute("person");
			
			int caDevId = 0;
			String id = request.getParameter("id");
			if(id.equalsIgnoreCase("_empty")) {
				String goal = request.getParameter("goal");
				String action = request.getParameter("action");
				String target = request.getParameter("target");
				String complete = request.getParameter("complete");
				String comment = request.getParameter("comment");
				String status = request.getParameter("status");
				
				Date targetD = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(target);
				Date completeD = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(complete);
			
				CareerDev caDev = new CareerDev(0, goal, action, targetD, completeD, comment, status, em);
				
				System.out.println(pDao.addCareerDev(caDev));
			} 
			else {
				caDevId = Integer.parseInt(id);
				String goal = request.getParameter("goal");
				String action = request.getParameter("action");
				String target = request.getParameter("target");
				String complete = request.getParameter("complete");
				String comment = request.getParameter("comment");
				String status = request.getParameter("status");
				
				Date targetD = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(target);
				Date completeD = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(complete);
			
				CareerDev caDev = new CareerDev(caDevId, goal, action, targetD, completeD, comment, status, em);
				
				System.out.println(pDao.editCareerDev(caDev));
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
